package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.History;
import com.kun.index.entity.RateUserBlog;
import com.kun.index.mapper.RateUserBlogMapper;
import com.kun.index.service.RateUserBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户博客评分表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-03-26
 */
@Service
public class RateUserBlogServiceImpl extends ServiceImpl<RateUserBlogMapper, RateUserBlog> implements RateUserBlogService {

    @Autowired
    private RateUserBlogMapper rateUserBlogMapper;


    @Override
    public RateUserBlog selectByRateUserBlog(RateUserBlog rateUserBlog) {
        QueryWrapper<RateUserBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",rateUserBlog.getUserId());
        wrapper.eq("blogId",rateUserBlog.getBlogId());
        return rateUserBlogMapper.selectOne(wrapper);
    }

    @Override
    public int updateRateUserBlog(RateUserBlog rateUserBlog) {
        QueryWrapper<RateUserBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",rateUserBlog.getUserId());
        wrapper.eq("blogId",rateUserBlog.getBlogId());
        return rateUserBlogMapper.update(rateUserBlog,wrapper);
    }

    @Override
    public int addRateUserBlog(RateUserBlog rateUserBlog) {
        return rateUserBlogMapper.insert(rateUserBlog);
    }

    @Override
    public Map<String, Object> queryRateByCondition(HashMap<String, Object> map) {
        QueryWrapper<RateUserBlog> wrapper = new QueryWrapper<>();
        HashMap<String, Object> resultMap = new HashMap<>();


        //根据userId进行查询
        if ( map.get("userId")!=null){
            wrapper.eq("userId",(Integer)map.get("userId"));
        }
        //根据userId进行查询
        if ( map.get("blogId")!=null){
            wrapper.eq("blogId",(Integer)map.get("blogId"));
        }
        //根据评分降序
        if (map.get("rateScore_desc")!=null){
            wrapper.orderByDesc("rateScore");
        }
        //分页查询
        if (map.get("pageIndex")!=null && map.get("pageSize")!=null){
            Page<RateUserBlog> page = new Page((Integer) map.get("pageIndex"),(Integer) map.get("pageSize"));
            Page<RateUserBlog> selectPage = rateUserBlogMapper.selectPage(page, wrapper);
            List<RateUserBlog> list = selectPage.getRecords();
            long size = selectPage.getTotal();
            resultMap.put("rates",list);
            resultMap.put("size",size);
        }else{
            List<RateUserBlog> rateUserBlogs = rateUserBlogMapper.selectList(wrapper);
            resultMap.put("rates",rateUserBlogs);
            resultMap.put("size",rateUserBlogs.size());
        }
        return resultMap;
    }

    @Override
    public Integer deleteRate(RateUserBlog rateUserBlog) {
        QueryWrapper<RateUserBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",rateUserBlog.getUserId());
        wrapper.eq("blogId",rateUserBlog.getBlogId());
        return rateUserBlogMapper.delete(wrapper);

    }


}
